package com.cloudm.technician.ui.modules.cloudm_box.machine_list

import android.app.Dialog
import android.content.DialogInterface
import android.os.Bundle
import com.cloudm.technician.R
import com.cloudm.technician.data.model.DeviceInfo
import com.cloudm.technician.ui.base.BaseDialogFragment

/**
 * 作者: wesley
 * 时间: 2017/12/18
 * 描述: 删除对话框
 */
class MachineListDialogFragment : BaseDialogFragment(), DialogInterface.OnClickListener {

    private lateinit var info: DeviceInfo
    var listener: OnDialogItemClickListener? = null

    companion object {
        private const val INFO = "info"
        fun newInstance(info: DeviceInfo) = MachineListDialogFragment().apply {
            arguments = Bundle().apply {
                putParcelable(INFO, info)
            }
        }
    }

    override fun handleBundle(bundle: Bundle) {
        info = bundle.getParcelable(INFO)
    }

    override fun createDialog(): Dialog {
        return android.support.v7.app.AlertDialog.Builder(mContext).setMessage(
                mContext.resources.getString(R.string.make_sure_delete_device,
                        info.deviceName)).setPositiveButton(R.string.delete,
                this).setNegativeButton(R.string.cancel, this).create()
    }


    override fun onClick(dialog: DialogInterface?, which: Int) {
        dismiss()
        when (which) {
            -1 -> listener?.onDeleteClick(info)
        }
    }

    override fun onDetach() {
        super.onDetach()
        listener = null
    }


    interface OnDialogItemClickListener {

        fun onDeleteClick(info: DeviceInfo)
    }
}